﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;

namespace Documentanalysis
{
    public partial class Main : Form
    {
        public List<string> folderArray = new List<string>();
        public string searchCondition = "";
        public string pathMask = "";
        public string fileMask = "";
        public Main()
        {
            InitializeComponent();
        }

        private void menuAnalysis_Click(object sender, EventArgs e)
        {
            FolderSelect folderSelect = new FolderSelect();
            folderSelect.Owner = this;
            if (folderSelect.ShowDialog() == DialogResult.OK)
            {
                Task task = new Task();
                task.folderArray = folderArray;
                task.pathMask = pathMask;
                task.fileMask = fileMask;
                tabControl1.TabPages.Add("任务");
                tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
                task.FormBorderStyle = FormBorderStyle.None;
                task.Dock = DockStyle.Fill;
                task.TopLevel = false;
                task.Show();
                task.Parent = tabControl1.SelectedTab;
            }
        }

        private void menuRefresh_Click(object sender, EventArgs e)
        {
            //
        }

        private void menuExport_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex >= 0)
            {
                if (tabControl1.SelectedTab.Text.Equals("任务"))
                {
                    SaveFileDialog sflg = new SaveFileDialog();
                    sflg.Filter = "文本文件(*.txt)|*.txt";
                    if (sflg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                    {
                        return;
                    }

                    // 写入txt文件
                    {
                        StreamWriter sw = new StreamWriter(sflg.FileName, true, Encoding.GetEncoding("gb2312"));
                        Document doc = ((Task)tabControl1.SelectedTab.Controls[0]).doc;
                        writeDocTxt(sw, doc, 0);
                        sw.Close();
                    }
                    // 写入excel文件
                    //{
                    //    NPOI.SS.UserModel.IWorkbook book = null;
                    //    if (sflg.FilterIndex == 1)
                    //    {
                    //        book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                    //    }
                    //    else
                    //    {
                    //        book = new NPOI.XSSF.UserModel.XSSFWorkbook();
                    //    }

                    //    NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("test_001");

                    //    Document doc = ((Task)tabControl1.SelectedTab.Controls[0]).doc;
                    //    writeDoc(sheet, doc, 0, 0);

                    //    // 写入 
                    //    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    //    book.Write(ms);
                    //    book = null;

                    //    using (FileStream fs = new FileStream(sflg.FileName, FileMode.Create, FileAccess.Write))
                    //    {
                    //        byte[] data = ms.ToArray();
                    //        fs.Write(data, 0, data.Length);
                    //        fs.Flush();
                    //    }

                    //    ms.Close();
                    //    ms.Dispose();
                    //}
                }
            }
        }

        private void menuSearch_Click(object sender, EventArgs e)
        {
            SearchSelect searchSelect = new SearchSelect();
            searchSelect.Owner = this;
            if (searchSelect.ShowDialog() == DialogResult.OK)
            {
                Select select = new Select();
                select.folderArray = folderArray;
                select.searchCondition = searchCondition;
                tabControl1.TabPages.Add("查询");
                tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
                select.FormBorderStyle = FormBorderStyle.None;
                select.Dock = DockStyle.Fill;
                select.TopLevel = false;
                select.Show();
                select.Parent = tabControl1.SelectedTab;
            }
        }

        private void writeDocTxt(StreamWriter sw, Document doc, int colIndex)
        {
            string s = getTab(colIndex) + doc.name;
            sw.WriteLine(s);

            writeAnalysisTxt(sw, doc, colIndex + 2);
            foreach (var son in doc.sons)
            {
                writeDocTxt(sw, son, colIndex + 1);
            }

        }

        private string getTab(int index)
        {
            string s = "";
            for (int i = 0; i < index; i++)
            {
                s += "  ";
            }

            return s;
        }

        private int writeDoc(ISheet sheet, Document doc, int rowIndex, int colIndex)
        {
            NPOI.SS.UserModel.IRow row = sheet.CreateRow(rowIndex);
            NPOI.SS.UserModel.ICell cell = row.CreateCell(colIndex);
            cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
            cell.SetCellValue(doc.name);

            rowIndex = writeAnalysis(sheet, doc, rowIndex, colIndex + 2);

            foreach (var son in doc.sons)
            {
                rowIndex++;
                rowIndex = writeDoc(sheet, son, rowIndex, colIndex + 1);
            }

            return rowIndex;
        }

        private void writeAnalysisTxt(StreamWriter sw, Document doc, int colIndex)
        {
            string s = "";
            s = getTab(colIndex) + "本级文件夹数量" + getTab(1) + doc.currentFolderCount;
            sw.WriteLine(s);

            s = getTab(colIndex) + "本级文件数量" + getTab(1) + doc.currentFileCount;
            sw.WriteLine(s);

            foreach (KeyValuePair<string, int> kvp in doc.currentTypes)
            {
                s = getTab(colIndex) + kvp.Key + getTab(1) + kvp.Value;
                sw.WriteLine(s);
            }

            s = getTab(colIndex) + "全部文件夹数量" + getTab(1) + doc.totalFolderCount;
            sw.WriteLine(s);

            s = getTab(colIndex) + "全部文件数量" + getTab(1) + doc.totalFileCount;
            sw.WriteLine(s);

            foreach (KeyValuePair<string, int> kvp in doc.totalTypes)
            {
                s = getTab(colIndex) + kvp.Key + getTab(1) + kvp.Value;
                sw.WriteLine(s);
            }
        }

        private int writeAnalysis(ISheet sheet, Document doc, int rowIndex, int colIndex)
        {
            {
                rowIndex++;
                IRow row = sheet.CreateRow(rowIndex);
                {
                    ICell cell = row.CreateCell(colIndex);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue("本级文件夹数量");
                }
                {
                    ICell cell = row.CreateCell(colIndex + 1);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue(doc.currentFolderCount);
                }
            }

            {
                rowIndex++;
                IRow row = sheet.CreateRow(rowIndex);
                {
                    ICell cell = row.CreateCell(colIndex);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue("本级文件数量");
                }
                {
                    ICell cell = row.CreateCell(colIndex + 1);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue(doc.currentFileCount);
                }
            }

            foreach (KeyValuePair<string, int> kvp in doc.currentTypes)
            {
                rowIndex++;
                IRow row = sheet.CreateRow(rowIndex);
                {
                    ICell cell = row.CreateCell(colIndex);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue(kvp.Key);
                }
                {
                    ICell cell = row.CreateCell(colIndex + 1);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue(kvp.Value);
                }
            }

            {
                rowIndex++;
                IRow row = sheet.CreateRow(rowIndex);
                {
                    ICell cell = row.CreateCell(colIndex);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue("全部文件夹数量");
                }
                {
                    ICell cell = row.CreateCell(colIndex + 1);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue(doc.totalFolderCount);
                }
            }

            {
                rowIndex++;
                IRow row = sheet.CreateRow(rowIndex);
                {
                    ICell cell = row.CreateCell(colIndex);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue("全部文件数量");
                }
                {
                    ICell cell = row.CreateCell(colIndex + 1);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue(doc.totalFileCount);
                }
            }

            foreach (KeyValuePair<string, int> kvp in doc.totalTypes)
            {
                rowIndex++;
                IRow row = sheet.CreateRow(rowIndex);
                {
                    ICell cell = row.CreateCell(colIndex);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue(kvp.Key);
                }
                {
                    ICell cell = row.CreateCell(colIndex + 1);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.STRING);
                    cell.SetCellValue(kvp.Value);
                }
            }

            return rowIndex;
        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.tabControl1.Padding = new System.Drawing.Point(10, 2);
            this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);
            this.tabControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseDown);
        }

        const int CLOSE_SIZE = 15;

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {
                Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index);

                //先添加TabPage属性   
                e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.tabControl1.Font, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);

                //再画一个矩形框
                using (Pen p = new Pen(Color.White))
                {
                    myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
                    myTabRect.Width = CLOSE_SIZE;
                    myTabRect.Height = CLOSE_SIZE;
                    e.Graphics.DrawRectangle(p, myTabRect);
                }

                //填充矩形框
                Color recColor = e.State == DrawItemState.Selected ? Color.White : Color.White;
                using (Brush b = new SolidBrush(recColor))
                {
                    e.Graphics.FillRectangle(b, myTabRect);
                }

                //画关闭符号
                using (Pen objpen = new Pen(Color.Black))
                {
                    ////=============================================
                    //自己画X
                    ////"\"线
                    //Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
                    //Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
                    //e.Graphics.DrawLine(objpen, p1, p2);
                    ////"/"线
                    //Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
                    //Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
                    //e.Graphics.DrawLine(objpen, p3, p4);

                    ////=============================================
                    //使用图片
                    Bitmap bt = new Bitmap(imageList1.Images[0]);
                    Point p5 = new Point(myTabRect.X, 4);
                    e.Graphics.DrawImage(bt, p5);
                    //e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.Font, objpen.Brush, p5);
                }
                e.Graphics.Dispose();
            }
            catch (Exception)
            { }
        }

        //关闭按钮功能
        private void tabControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int x = e.X, y = e.Y;
                //计算关闭区域   
                Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex);

                myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
                myTabRect.Width = CLOSE_SIZE;
                myTabRect.Height = CLOSE_SIZE;

                //如果鼠标在区域内就关闭选项卡   
                bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
                if (isClose == true)
                {
                    
                    this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
                }
            }
        }
       
    }
}
